package StudyCode.summary.mycollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class IterableCastToCollection {
	public static <T> Collection<T> asCollection(Iterable<T> iterable) {
		return (iterable instanceof Collection) ? (Collection<T>) iterable
				: shallowCopy(iterable);
	}


	public static <T> List<T> shallowCopy(Iterable<T> ori) {
		ArrayList<T> copy = new ArrayList<T>();

		for (T t : ori) {
			copy.add(t);
		}

		return copy;
	}


	public static <T> List<T> shallowCopy(Iterator<T> iter) {
		ArrayList<T> copy = new ArrayList<T>();

		while (iter.hasNext()) {
			copy.add(iter.next());
		}

		return copy;
	}
}
